//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FixedRateBondForward.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Instruments/Bonds/FixedRateBond.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/InterestRate.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/Forward.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Instruments::Bonds;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CFixedRateBondForward::CFixedRateBondForward (DateTime valueDate, DateTime maturityDate, QL::Position::TypeEnum type, Double strike, UInt32 settlementDays, Cephei::QL::Times::IDayCounter^ dayCounter, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum businessDayConvention, Cephei::QL::Instruments::Bonds::IFixedRateBond^ fixedCouponBond, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ incomeDiscountCurve, Cephei::QL::IPricingEngine^ QL_Pricer) : CForward(CFixedRateBondForward::typeid)
{
    CDayCounter^ _CdayCounter;
    CCalendar^ _Ccalendar;
    CFixedRateBond^ _CfixedCouponBond;
    CYieldTermStructure^ _CdiscountCurve;
    CYieldTermStructure^ _CincomeDiscountCurve;
    try
    {
#ifdef HANDLE
        _phFixedRateBondForward = NULL;
#endif
        QuantLib::Date _valueDate = (QuantLib::Date)ValueHelper::Convert (valueDate);
        QuantLib::Date _maturityDate = (QuantLib::Date)ValueHelper::Convert (maturityDate);
        QuantLib::Position::Type _type = (QuantLib::Position::Type)type ;
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike);
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _businessDayConvention = (QuantLib::BusinessDayConvention)businessDayConvention ;
        _CfixedCouponBond = safe_cast<CFixedRateBond^> (fixedCouponBond);
        _CfixedCouponBond->Lock();
        boost::shared_ptr<QuantLib::FixedRateBond>& _fixedCouponBond = static_cast<boost::shared_ptr<QuantLib::FixedRateBond>&> (_CfixedCouponBond->GetShared ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountCurve))
        {
            _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve->Value);
            _CdiscountCurve->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _discountCurve = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountCurve) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (incomeDiscountCurve))
        {
            _CincomeDiscountCurve = safe_cast<CYieldTermStructure^> (incomeDiscountCurve->Value);
            _CincomeDiscountCurve->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _incomeDiscountCurve = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (incomeDiscountCurve) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CincomeDiscountCurve->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppFixedRateBondForward = new boost::shared_ptr<QuantLib::FixedRateBondForward> (new QuantLib::FixedRateBondForward ( _valueDate,  _maturityDate,  _type,  _strike,  _settlementDays,  _dayCounter,  _calendar,  _businessDayConvention,  _fixedCouponBond,  _discountCurve,  _incomeDiscountCurve ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppFixedRateBondForward)->setPricingEngine (_QL_Pricer);
        SetForward (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppFixedRateBondForward));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CfixedCouponBond != nullptr) _CfixedCouponBond->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
        if (_CincomeDiscountCurve != nullptr) _CincomeDiscountCurve->Unlock();
    }
}
Cephei::QL::Instruments::CFixedRateBondForward::CFixedRateBondForward (boost::shared_ptr<QuantLib::FixedRateBondForward>& childNative, Object^ owner) : CForward(CFixedRateBondForward::typeid)
{
#ifdef HANDLE
	_phFixedRateBondForward = NULL;
#endif
	_ppFixedRateBondForward = &childNative;
    _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppFixedRateBondForward));
}
Cephei::QL::Instruments::CFixedRateBondForward::CFixedRateBondForward (QuantLib::FixedRateBondForward& childNative, Object^ owner) : CForward(CFixedRateBondForward::typeid)
{
#ifdef HANDLE
	_phFixedRateBondForward = NULL;
#endif
	_ppFixedRateBondForward = new boost::shared_ptr<QuantLib::FixedRateBondForward> (&childNative);
    _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppFixedRateBondForward));
    _FixedRateBondForwardOwner = owner;
    _ForwardOwner = owner;
}

Cephei::QL::Instruments::CFixedRateBondForward::CFixedRateBondForward (CFixedRateBondForward^ copy) : CForward(CFixedRateBondForward::typeid)
{
#ifdef HANDLE
	_phFixedRateBondForward = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFixedRateBondForward = new boost::shared_ptr<QuantLib::FixedRateBondForward> (copy->GetShared());
        _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppFixedRateBondForward));
    }
}
Cephei::QL::Instruments::CFixedRateBondForward::CFixedRateBondForward (System::Type^ t) : CForward(CFixedRateBondForward::typeid)
{
#ifdef HANDLE
	_phFixedRateBondForward = NULL;
#endif
	if (!t->IsSubclassOf(CFixedRateBondForward::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CFixedRateBondForward::CFixedRateBondForward (QuantLib::Handle<QuantLib::FixedRateBondForward>& childNative, Object^ owner)  : CForward(CFixedRateBondForward::typeid)
{
	_phFixedRateBondForward = &childNative;
	_ppFixedRateBondForward = &static_cast<boost::shared_ptr<QuantLib::FixedRateBondForward>>(childNative.currentLink());
    _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppFixedRateBondForward));
    _FixedRateBondForwardOwner = owner;
}
Cephei::QL::Instruments::CFixedRateBondForward::CFixedRateBondForward (QuantLib::Handle<QuantLib::FixedRateBondForward> childNative)  : CForward(CFixedRateBondForward::typeid)
{
	_phFixedRateBondForward = &childNative;
	_ppFixedRateBondForward = &static_cast<boost::shared_ptr<QuantLib::FixedRateBondForward>>(childNative.currentLink());
    _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppFixedRateBondForward));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CFixedRateBondForward::CFixedRateBondForward (QuantLib::FixedRateBondForward childNative)  : CForward(CFixedRateBondForward::typeid)
{
#ifdef HANDLE
	_phFixedRateBondForward = NULL;
#endif
	_ppFixedRateBondForward = new boost::shared_ptr<QuantLib::FixedRateBondForward> (new QuantLib::FixedRateBondForward (childNative));
    _ppForward = new boost::shared_ptr<QuantLib::Forward> (boost::dynamic_pointer_cast<QuantLib::Forward> (*_ppFixedRateBondForward));
}
#endif

Cephei::QL::Instruments::CFixedRateBondForward::~CFixedRateBondForward ()
{
    if (_ppFixedRateBondForward != NULL)
    {
	    delete _ppFixedRateBondForward;
        _ppFixedRateBondForward = NULL;
    }
}
Cephei::QL::Instruments::CFixedRateBondForward::!CFixedRateBondForward ()
{
    if (_ppFixedRateBondForward != NULL)
    {
	    delete _ppFixedRateBondForward;
    }
}
QuantLib::FixedRateBondForward& Cephei::QL::Instruments::CFixedRateBondForward::GetReference ()
{
    if (_ppFixedRateBondForward == NULL) throw gcnew NativeNullException ();
	return **_ppFixedRateBondForward;
}
boost::shared_ptr<QuantLib::FixedRateBondForward>& Cephei::QL::Instruments::CFixedRateBondForward::GetShared ()
{
    if (_ppFixedRateBondForward == NULL) throw gcnew NativeNullException ();
	return *_ppFixedRateBondForward;
}
QuantLib::FixedRateBondForward* Cephei::QL::Instruments::CFixedRateBondForward::GetPointer ()
{
    if (_ppFixedRateBondForward == NULL) throw gcnew NativeNullException ();
	return &**_ppFixedRateBondForward;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FixedRateBondForward>& Cephei::QL::Instruments::CFixedRateBondForward::GetHandle ()
{
	if (_phFixedRateBondForward == NULL)
	{
		_phFixedRateBondForward = new Handle<QuantLib::FixedRateBondForward> (*_ppFixedRateBondForward);
	}
	return *_phFixedRateBondForward;
}
#endif
bool Cephei::QL::Instruments::CFixedRateBondForward::HasNative () 
{
	return (_ppFixedRateBondForward != NULL);
}

Double Cephei::QL::Instruments::CFixedRateBondForward::CleanForwardPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFixedRateBondForward)->cleanForwardPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CFixedRateBondForward::ForwardPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFixedRateBondForward)->forwardPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CFixedRateBondForward::SpotIncome (Cephei::QL::Termstructures::IYieldTermStructure^ incomeDiscountCurve)
{
    CYieldTermStructure^ _CincomeDiscountCurve;
    try
    {
        _CincomeDiscountCurve = safe_cast<CYieldTermStructure^> (incomeDiscountCurve);
        _CincomeDiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _incomeDiscountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CincomeDiscountCurve->GetHandle ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFixedRateBondForward)->spotIncome ( _incomeDiscountCurve );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CincomeDiscountCurve != nullptr) _CincomeDiscountCurve->Unlock();
    }
}
Double Cephei::QL::Instruments::CFixedRateBondForward::SpotValue::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFixedRateBondForward)->spotValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IFixedRateBondForward^ Cephei::QL::Instruments::CFixedRateBondForward_Factory::Create (DateTime valueDate, DateTime maturityDate, QL::Position::TypeEnum type, Double strike, UInt32 settlementDays, Cephei::QL::Times::IDayCounter^ dayCounter, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum businessDayConvention, Cephei::QL::Instruments::Bonds::IFixedRateBond^ fixedCouponBond, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ incomeDiscountCurve, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CFixedRateBondForward ( valueDate,  maturityDate,  type,  strike,  settlementDays,  dayCounter,  calendar,  businessDayConvention,  fixedCouponBond,  discountCurve,  incomeDiscountCurve,  QL_Pricer);
}
